package com.group9.handinhand.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.group9.handinhand.dao.model.Friendship;
import com.group9.handinhand.dao.model.FriendshipRequest;
import com.group9.handinhand.dao.model.PasswordResetLink;
import com.group9.handinhand.dao.model.User;
import com.group9.handinhand.service.so.UserSO;

@Repository
public class UserDao extends BaseDaoImpl {
	
	public List<User> findAllUsers() {		
		return findAll(User.class);
	}

	public User saveUser(User user) {
		save(user);	
		return user;
	}
	
	public User updateUser(User user) {
		update(user);
		return user;
	}
	/**
	 * finds user by id
	 * @param userId
	 * @return
	 */
	public User findUserById(Long userId) {
		return (User) findById(User.class, userId);
	}
	/**
	 * finds user by email adddress
	 * @param email
	 * @return
	 */
	public User findUserByEmail(String email) {
		return findEntityByPropertyUnique(User.class, "email", email);
	}
	/**
	 * finds user by user name
	 * @param username
	 * @return
	 */
	public User findUserByUsername(String username) {
		return findEntityByPropertyUnique(User.class, "username", username);
	}
	/**
	 * finds the friendship between users
	 * @param firstUserId
	 * @param secondUserId
	 * @return
	 */
	public Friendship findFriendship(Long firstUserId, Long secondUserId) {
		String queryString = "from Friendship as fr where fr.firstUser.id =:firstUserId and fr.secondUser.id =:secondUserId ";
		return executeQueryStringForObject(queryString, new String[]{"firstUserId","secondUserId"},new Object[]{firstUserId,secondUserId});	
	}
	/**
	 * saves the friendship request
	 * @param model
	 * @return
	 */
	public FriendshipRequest saveFriendshipRequest(FriendshipRequest model) {
		save(model);
		return model;
	}
	/***
	 * finds all the friendship request related to a user
	 * @param userId
	 * @return
	 */
	public List<FriendshipRequest> findAllFriendshipRequestsByUserId(Long userId){
		String queryString = "from FriendshipRequest as fr where fr.receiver.id =:userId";
		return executeQueryString(queryString, new String[]{"userId"},new Object[]{userId});		
	}
	/**
	 * finds all friendships of user
	 * @param userId
	 * @return
	 */
	public List<Friendship> findAllFriendshipsByUserId(Long userId){
		String queryString = "from Friendship as fr where fr.firstUser.id =:userId or fr.secondUser.id =:userId";
		return executeQueryString(queryString, new String[]{"userId"},new Object[]{userId});		
	}
	/**
	 * finds a friendship request between users using id
	 * @param senderId
	 * @param receiverId
	 * @return
	 */
	public FriendshipRequest findFriendshipRequestBySenderIdAndReceiverId(Long senderId, Long receiverId) {
		String queryString = "from FriendshipRequest as fr where fr.sender.id =:senderId and fr.receiver.id =:receiverId ";
		return executeQueryStringForObject(queryString, new String[]{"senderId","receiverId"},new Object[]{senderId,receiverId});	
	}

	public Friendship saveFriendship(Friendship model) {
		save(model);
		return model;
	}

	public void deleteFriendshipRequest(FriendshipRequest model) {
		delete(model);
	}

	public void deleteFriendship(Friendship model) {
		delete(model);
	}

	public PasswordResetLink savePasswordResetLink(PasswordResetLink model) {
		save(model);
		return model;
	}

	public PasswordResetLink findPasswordResetLinkByLink(String link) {
		return findEntityByPropertyUnique(PasswordResetLink.class, "link", link);
	}

	public List<PasswordResetLink> findPasswordResetLinksByUserId(Long userId) {
		String queryString = "from PasswordResetLink as prl where prl.user.id =:userId";
		return executeQueryString(queryString, new String[]{"userId"},new Object[]{userId});
	}

	public void deletePasswordResetLink(PasswordResetLink model) {
		delete(model);
	}

	public User deletePicture(User user) {
		update(user);
		return user;
	}

}
